package com.um.jdy.ykc.command;

import com.um.jdy.ykc.utils.RadixsUtil;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;

/**
 * 离线卡数据清除（运营平台->充电桩）
 *
 * @author 甘天伟
 * @since 2022/6/28
 * 版权所有 广州优匠科技有限公司
 */
@Data
public class Cmd46RemoveCardsRequest extends YkcCommand {

    /**
     * 桩编码：7个字节
     */
    private String code;

    /**
     * 清除离线卡的个数：1个字节
     */
    private int cardNum;

    /**
     * 物理卡号：8个字节
     */
    private List<String> cardList;

    public Cmd46RemoveCardsRequest(int serial,String code,int cardNum,List<String> cardList) {
        super();

        this.serial = serial;
        this.encFlag = RadixsUtil.hexToInteger("00");
        this.command = RadixsUtil.hexToInteger("46");

        this.code = code;
        this.cardNum = cardNum;
        this.cardList = cardList;

        byte[] codeByte = RadixsUtil.hexToBytes(code);
        byte cardNumByte = (byte)cardNum;
        List<byte[]> cardBytes = new ArrayList<>();

        for(int index=0;index<cardList.size();index++){
            String card = cardList.get(index);
            byte[] cardByte = RadixsUtil.hexToBytes(card);
            cardBytes.add(fillZeroBytes(cardByte,8-cardByte.length));
        }

        ByteBuf buff = Unpooled.buffer();
        buff.writeBytes(fillZeroBytes(codeByte,7 - codeByte.length));
        buff.writeByte(cardNumByte);
        for(byte[] cardByte : cardBytes){
            buff.writeBytes(cardByte);
        }

        this.data = new byte[buff.readableBytes()];
        buff.readBytes(data);
        buff.release();

        postConstruct();
    }

}
